package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xymarket.common.Info;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.httpUtil.MerchantService;
import com.example.xymarket.R;

public class MineActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        StatusBar.setStatusBarColor(MineActivity.this, StatusBar.yellow);
        TextView text = (TextView) findViewById(R.id.nickname);
        ImageView imageView =(ImageView) findViewById(R.id.avatar);
        if (Info.getMyUserInfo()==null) {
            text.setText("请登录");
        }
        else
        {
            text.setText(Info.getMyUserInfo().getName());
            imageView.setImageDrawable(Info.getMyUserAvatar());
        }
        if(Info.getMyMerchant()==null)
        {
            initMyMerchant();
        }
        else
        {
            handler2.sendEmptyMessage(10);
        }
    }

    public void initMyMerchant()
    {
        MyHandler myHandler=new MyHandler(this,handler2);
        MerchantService merchantService=new MerchantService(myHandler);
        merchantService.getMyMerchant();

    }

    @SuppressLint("HandlerLeak")
    final Handler handler2 = new Handler()
    {
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            if(msg.what==10) {
                TextView textView=(TextView) findViewById(R.id.order_name);
                textView.setText(Info.getMyMerchant().getName());
                ImageView imageView=(ImageView) findViewById(R.id.order_avatar);
                imageView.setImageDrawable(Info.getMyMerchant().getDrawable());
            }
        }
    };

    public void toHome(View view)
    {
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }


    public void toShoppingCart(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ShoppingCartActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toClassify(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ClassifyActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toLogin(View view)
    {
        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }


    public void toOrder(View view)
    {
        Intent intent = new Intent(getApplicationContext(), OrderActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toImages(View view)
    {
        Intent intent = new Intent(getApplicationContext(), ImagesActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toMyMerchant(View view)
    {
        Intent intent = new Intent(getApplicationContext(), MyShopActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    public void toAddress(View view)
    {
        Intent intent = new Intent(getApplicationContext(), AddressActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);
    }

    /**
     * 再按一次退出程序
     * 用时间来计算两次返回之间的时间
     */
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}